<?php

namespace Admin\Controller;

class BookingController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Booking");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();

        $requestMap["b.status"] = 1;

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        //获取order列表
        $orderList = $this->currentModel->queryForList($page, $requestMap);

        $this->assign('orderList', $orderList);
        $this->assign('bookingStatusEnum', C("BOOKING_STATUS"));
        $this->assign('bookingTypeEnum', C("BOOKING_TYPE"));
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    public function settingDay() {
        $this->display();
    }

    public function settingDayPost() {

        $day = $_REQUEST["day"];

        $bookingProductMap["c.is_finished"] = -1;
        $bookingProductList = D("Product")->alias("p")
            ->join("left join __CATEGORY__ c on c.category_id = p.category_id")
            ->where($bookingProductMap)
            ->field("p.*")
            ->select();

        foreach($bookingProductList as $product) {
            $map["day"] = $day;
            $map["product_id"] = $product["product_id"];

            $exist = D("BookingLog")->where($map)->find();

            if(!isNullOrEmpty($exist)) {
                $exist["status"] = -1;
                D("BookingLog")->updateInfo($exist);
            } else {
                $bookingData["product_id"] = $product["product_id"];
                $bookingData["day"] = $day;
                $bookingData["status"] = -1;
                $bookingData["number"] = 0;
                $bookingData["create_time"] = getCurrentDate();
                $bookingData["shop_id"] = 1;
                D("BookingLog")->add($bookingData);
            }
        }

        $this->success("设置成功", U("booking/settingDay"));

    }

    public function endService() {

        $id = $_REQUEST["id"];

        $booking = $this->currentModel->queryById($id);
        $booking["status"] = C("BOOKING_STATUS.END");

        $this->currentModel->updateInfo($booking);

        $this->success("已标记预约完成", U("booking/index"));

    }

}